<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="net.pws.oos.security.SecurityUtils"%>
<%@ include file="../../header.jsp"%>

<div class="border bg">
<div class="page-header">
	<h2>派发任务列表<small>(<span id="currentCount">0</span>/<span id="totalCount">0</span>)</small></h2>
	<div class="cms_toolbar">
		<% if(SecurityUtils.allowedResource("/task/dispatch/create")){ %>
		<a id="_create_item_action" href="edit.do" class="btn"><i class="icon-plus"></i>新建</a>
		<%} %>
		
<!-- 		<a id="importButton" href="#" onclick="importTaskByExl()" class="btn"><i class="icon-share"></i>导入</a> -->
		<a id="importButton" href="#" onclick="exportExcel()" class="btn"><i class="icon-share"></i>导入</a>
		
	</div>
</div>

<div class="text-right form-inline">
	<select name="status" id="status" class="input-small">
		<option value="">全部</option>
		<option value="assigned" selected="selected">待办</option>
		<option value="in_process">处理中</option>
		<option value="completed">已完成</option>
	</select>
	&nbsp;
	<input type="text" name="projectId" id="projectId" class="input-small" readonly="readonly" onclick="selectProject()" placeholder="请选择项目..."/>
	<label class="control-label" for="createStartDate">&nbsp;创建从</label>
	<input type="text" name="createStartDate" id="createStartDate" class="input-small" value="${createStartDate}" />
	<label class="control-label" for="createEndDate">到</label>
	<input type="text" name="createEndDate" id="createEndDate" class="input-small" value="${createEndDate}" />
	&nbsp;
	<input type="text" name="name" id="name" class="input-small" placeholder="请输入关键词..."/>
	
	<button class="btn" onclick="refresh()">查询</button>
</div>

<div>
	<br />
	<table id="listTable" class="table table-striped cms-table">
		<thead>
			<tr>
				<th>名称</th>
				<th width="80">项目</th>
				<th width="40">状态</th>
				<th width="60">处理人</th>
				<th width="70">定位时间</th>
				<th width="60">创建人</th>
				<th width="70">下发时间</th>
				<th width="60">操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	<div class="well cms_more">
		<a href="javascript:;" onclick="moreData()"><i class="icon-arrow-down"></i>查看更多...</a>
	</div>
</div>

</div>


</div>

<table id="taskTemplate" class="hide">
  <tr>
    <td><a href="" target="blank"></a></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><a href="" class="hide">编辑</a> <a href="" class="hide">导出</a></td>
  </tr>
</table>


<script type="text/javascript">
var pageIndex = 0, pageSize = 20, currentCount = 0, totalCount = 0;

//导入
function importTaskByExl() {
	$.openDialog("#dialog",
			{height:350,
			width:300,
			minHeight:350,
			minWidth:300,
			loadPage:ctx + "/biz/task/import/excel.jsp",
			title:"导入任务"},
			function(data) {
				alert(data);
				$("#taskExecPerson").data("id",data.data("id"));
				$("#taskExecPerson").val(data.data("name"));
			});
}

function exportExcel(){
	
// 	var url = "statistics/excel.do?status=completed&locateStartDate=" + $("#locateStartDate").val();
// 	url = url + "&locateEndDate=" + $("#locateEndDate").val();
// 	if($("#executor").data("type") == "user"){
// 		url = url + "&execPersonId=" + $("#executor").data("id");
// 	}
// 	if($("#executor").data("type") == "organ"){
// 		url = url + "&execOrganId=" + $("#executor").data("id");
// 	}
	var url = ctx + "/biz/task/import/excel.jsp";
	window.open(url);
}

function selectProject() {
	$.openDialog("#dialog",
			{height:550,
			width:800,
			minHeight:450,
			minWidth:600,
			loadPage:ctx + "/biz/project/query.do",
			title:"选择项目"},
			function(data) {
				$("#projectId").data("id",data.id);
				$("#projectId").val(data.name);
			});
}

function getParameter(){
	return {
		start: pageIndex,
		limit: pageSize,
		createType : "dispatch",
		status: $("#status").val(),
		createStartDate: $("#createStartDate").val(),
		createEndDate: $("#createEndDate").val(),
		projectId: $("#projectId").data("id"),
		name: $("#name").val()
	};
}

function count(){
	block(".cms_more", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/task/count.json",
    	data: getParameter(),
    	dataType:"json",
    	success:function(json) {
    		unblock(".cms_more");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
    		totalCount = json.data;
			$("#totalCount").text(json.data);
			list();
    	},
		error: function(message) {
			unblock(".cms_more");
			$.alert(message);
		}
	});
}

function list(){
	block(".cms_more", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/task/list.json",
    	data: getParameter(),
    	dataType:"json",
    	success:function(json) {
    		unblock(".cms_more");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
			buildList(json.data);
    	},
		error: function(message) {
			unblock(".cms_more");
			$.alert(message);
		}
	});
}

function refresh(){
	pageIndex = 0;
	currentCount = 0;
	totalCount = 0;
	
	$("#currentCount").text(currentCount);
	$("#totalCount").text(totalCount);
	$("#listTable").find("tbody").html("");
	$(".cms_more").show();
	count();
	//list();
}

function moreData(){
	pageIndex++;
	list();
}

function buildList(data){
	if(data.length == 0){
		$(".cms_more").hide();
		return;
	}
	currentCount += data.length;
	$("#currentCount").text(currentCount);
	if(currentCount >= totalCount){
		$(".cms_more").hide();
	} else {
		$(".cms_more").show();
	}
	$(data).each(function(index, item) {
		buildTd(item).appendTo($("#listTable").find("tbody"));
	});
}

function buildTd(item){
	var taskEle = $("#taskTemplate").clone();
	taskEle.find("tr").attr("id", "item" + item.id);
	var tdList = taskEle.find("td");
	$(tdList.get(0)).find("a").attr("href", ctx + "/biz/task/detail.do?id=" + item.id);
	$(tdList.get(0)).find("a").text(item.name);
	$(tdList.get(1)).text(item.projectName == null ? "" : item.projectName);
	$(tdList.get(2)).text(getTaskStatus(item.status));
	$(tdList.get(3)).text(item.execPersonName);
	$(tdList.get(4)).text(item.locateDate == null ? "": item.locateDate);
	$(tdList.get(5)).text(item.creatorName);
	$(tdList.get(6)).text(item.createDateTime);
	
	// 操作项
	var operates = $(tdList.get(7)).find("a");
	if(item.status == "assigned"){
		$(operates.get(0)).removeClass("hide");
		$(operates.get(0)).attr("href", ctx + "/biz/task/edit.do?id=" + item.id);
	}
	if(item.status == "completed"){
		$(operates.get(1)).removeClass("hide");
		$(operates.get(1)).attr("href", ctx + "/biz/task/word.do?id=" + item.id);
	}
	
	return taskEle.find("tr");
}

$(document).ready(function() {
	//$("#taskPlanStartDate").datepicker();
	$("#createStartDate").datepicker();
	$("#createEndDate").datepicker();
	
	refresh();
});
</script>

<%@ include file="../../footer.jsp"%>